package com.cy1640.damai.yanchu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.cy1640.damai.R;
import com.cy1640.damai.adapter.MyItemBaseAdapter;
import com.cy1640.damai.bean.ParserJsonYanchu;
import com.cy1640.damai.bean.Yanchu;
import com.cy1640.damai.util.HttpUtils;
import com.cy1640.damai.util.NetWorkUtils;

import java.util.List;


/**
 * Created by chenyang on 2016/12/12.
 */

public class Yanchu_fg11_cartoon extends Fragment {
    private SwipeRefreshLayout refreshLayout;
    private ListView lv;
    private String path="http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=0&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852";
    private static final int SUCCESS=1;
    private static final int ERROR=2;
    private static final int OTHER=3;

    public Yanchu_fg11_cartoon() {
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Log.i("aaaa" +
                    "", "handleMessage: ");
            switch (msg.what) {
                case SUCCESS:
                    List<Yanchu> list= (List<Yanchu>) msg.obj;
                    MyItemBaseAdapter adapter=new MyItemBaseAdapter(getActivity(),list);
                    lv.setAdapter(adapter);
                    break;
                case ERROR:
                    String errorMsg= (String) msg.obj;
                    Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
                    break;
                case OTHER:
                    String info= (String) msg.obj;
                    Toast.makeText(getActivity(), info, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i("aaaaa", ":************onCreateView**************** ");
        View view=inflater.inflate(R.layout.yanchu_fragment,container,false);
        return  view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Log.i("aaaaa", "onViewCreated:**************************** ");
        super.onViewCreated(view, savedInstanceState);
        lv= (ListView) view.findViewById(R.id.lv_yanchu);
        refreshLayout= (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshView);

        refreshLayout.setColorSchemeColors(
                getResources().getColor(R.color.red),
                getResources().getColor(R.color.green),
                getResources().getColor(R.color.yellow)
        );

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (NetWorkUtils.isNetworkConnected(getContext())) {
                    Log.i("aaa", "run: msg.what=;");

                    byte[] buffer = HttpUtils.getString(path);
                    if (buffer != null && buffer.length != 0) {
                        String jsonString = new String(buffer, 0, buffer.length);
                        Log.i("aaa", "run: jsonString="+jsonString);
                        List<Yanchu> list = ParserJsonYanchu.parserJsonToList(jsonString);
                        Message msg = Message.obtain();
                        msg.what = SUCCESS;
                        msg.obj = list;
                        handler.sendMessage(msg);
                        Log.i("aaa","list....."+list);
                    } else {
                        Log.i("aaa", "run: msg.what=下载数据失败;");

                        Message msg = Message.obtain();
                        msg.what = OTHER;
                        msg.obj = "下载数据失败";
                        handler.sendMessage(msg);
                    }
                } else {
                    Log.i("aaa", "run: msg.what=ERROR;");
                    Message msg=Message.obtain();
                    msg.what=ERROR;
                    msg.obj="网络异常，请检查";
                    handler.sendMessage(msg);
                }
            }
        }).start();
    }

}
